package controllayer;

import java.sql.SQLException;
import java.util.ArrayList;

import dblayer.*;
import modellayer.Customer;
import modellayer.SalesOrder;
import modellayer.Supplier;

public class SupplierCtr {
	
	private IFDBSupplier dbSupplier;
	
	public SupplierCtr()
	{
		dbSupplier = new DBSupplier();
	}
	
	/**
	 * 
	 * @param supplierID
	 * @return
	 * @throws SQLException
	 */
	public Supplier findSupplier(int supplierID) throws SQLException
	{
		return dbSupplier.selectSingleSupplier(supplierID);
	}
	
	/**
	 * 
	 * @param name
	 * @param address
	 * @param country
	 * @param phoneNo
	 * @param email
	 * @param supplierID
	 * @throws SQLException
	 */
	public void createSupplier(String name, String address, String country,
			String phoneNo, String email) throws SQLException
	{
		Supplier s = new Supplier(name, address, country, phoneNo, email);
		dbSupplier.insertSupplier(s);
	}
	
	/**
	 * 
	 * @param supplierID
	 * @throws SQLException
	 */
	public void deleteSupplier(int supplierID) throws SQLException
	{
		Supplier s = dbSupplier.selectSingleSupplier(supplierID);
		dbSupplier.deleteSupplier(s);
	}
	
	/**
	 * 
	 * @param supplierID
	 * @throws SQLException
	 */
	public void updateSupplier(String name, String address, String country,
			String phoneNo, String email, int supplierID) throws SQLException
	{
		Supplier s = new Supplier(name, address, country, phoneNo, email, supplierID);
		dbSupplier.updateSupplier(s);
	}
	
	/**
	 * Adds all Supplier objects to a two dimensional array so they can be used in a JTable in ViewSupplierPanel
	 * @return
	 */
	 public String[][] supplierToTableArray()
	    {
		   ArrayList<Supplier> supplierList = dbSupplier.getAllSuppliers();
	    	
	       int index = 0; 
	       String data[][] = new String[supplierList.size()][6];
	       while(index < supplierList.size())
	       {
	    	   Supplier supplier = supplierList.get(index);
	    			   
	    	   data[index][0] = Integer.toString(supplier.getSupplierID());
	           data[index][1] = supplier.getName();
	           data[index][2] = supplier.getAddress();
	           data[index][3] = supplier.getCountry();
	           data[index][4] = supplier.getPhoneNo();
	           data[index][5] = supplier.getEmail();
	           index++;
	       }
	           
	      
	       return data;
	    }
	 
	 public String[] supplierToComboBox()
	 {
		 ArrayList<Supplier> supplierList = dbSupplier.getAllSuppliers();
		 
		 int index = 0;
		 String data[] = new String[supplierList.size()];
		 while(index < supplierList.size())
		 {
			 Supplier supplier = supplierList.get(index);
			 data[index] = supplier.getName(); 
			 
			 index++;
		 }
		 
		 return data;
	 }
	 
	 public Supplier getSupplier(String supplierName)
		{
			Supplier supplier = null;
			int index = 0;
			ArrayList<Supplier> supplierList = dbSupplier.getAllSuppliers();
			
			while(index < supplierList.size())
			{
				supplier = supplierList.get(index);
				if(supplier.getName().equals(supplierName)){
					break;
				}else {
					supplier = null;
				}
				index++;
			}
			
			
			return supplier;
		}
}
